import ts from 'rollup-plugin-typescript2';
import cjs from '@rollup/plugin-commonjs';
import path from 'node:path';
import fs from 'node:fs';
import replace from '@rollup/plugin-replace';

const pkgPath = path.resolve(__dirname, '../../packages');
const distPath = path.resolve(__dirname, '../../dist/node_modules');

export function resolvePackagePath(pkgName, isDist) {
	if (isDist) {
		return `${distPath}/${pkgName}`;
	}
	return `${pkgPath}/${pkgName}`;
}

export function getPackageJSON(pkgName) {
	// const path = `${reaolvePackageName(pkgName)}.package.json`
	const packagePath = path.resolve(resolvePackagePath(pkgName), 'package.json');
	const fileStr = fs.readFileSync(packagePath, { encoding: 'utf-8' });
	return JSON.parse(fileStr);
}

export function getBaseRollupPlugins({
	alias = {
		__DEV__: true,
		preventAssignment: true,
	},
	typescriptConfig = {},
} = {}) {
	return [replace(alias), cjs(), ts(typescriptConfig)];
}
